﻿@{
    ViewBag.Title = "ShopItemForm";
    Layout = "~/Views/Shared/_Form.cshtml";
}
<!--jqgrid表格组件start-->
<link href="~/Content/scripts/plugins/jqgrid/jqgrid.css" rel="stylesheet" />
<script src="~/Content/scripts/plugins/jqgrid/grid.locale-cn.js"></script>
<script src="~/Content/scripts/plugins/jqgrid/jqgrid.min.js"></script>
<!--表格组件end-->
<script>
    var keyValue = request('keyValue');
    var organizeId = request('organizeId');
    var $_rowid = request('rowid');
    var _ObjectTypeId = 0, _ObjectTypeName = "房间";
    var _rowid = 1;
    $(function () {
        $("#ObjectTypeId .dropdown-menu li").click(function () {
            var text = $(this).find('a').html();
            var value = $(this).find('a').attr('data-value');
            $("#ObjectTypeId .dropdown-text").html(text).attr('data-value', value);
            _ObjectTypeId = value;
            _ObjectTypeName = text;
            $('#btn_Search').trigger("click");
        });
        GetGrid();
        GetGrid1();
    });

    //加载表格
    function GetGrid() {
        var selectedRowIndex = 0;
        var queryJson = new Object();
        queryJson["organizeId"] = organizeId;
        queryJson["Type"] = _ObjectTypeId;
        var $gridTable = $('#gridTable');
        $gridTable.jqGrid({
            url: "/DailyEventManage/OpenShop/GetObject",
            postData: queryJson,
            datatype: "json",
            height: $(window).height() - 141.5,
            autowidth: true,
            colModel: [
                { label: "主键", name: "Id", index: "Id", hidden: true },
                { label: "编号", name: "Code", index: "Code", width: 70, align: "center" },
                { label: "名称", name: "Name", index: "Name", width: 100, align: "center" },
                { label: "位置描述", name: "OtherCode", index: "OtherCode", width: 50, align: "center" }
            ],
            rownumbers: true,
            rowNum: 1000,
            shrinkToFit: false,
            gridview: true,
            onSelectRow: function () {
                selectedRowIndex = $("#" + this.id).getGridParam('selrow');
            },
            gridComplete: function () {
                $("#" + this.id).setSelection(selectedRowIndex, false);
            },
            ondblClickRow: function (rowid, iRow, iCol, e) {
                var obj = $("#" + this.id).getRowData(rowid);
                obj["ObjectTypeId"] = _ObjectTypeId;
                obj["ObjectTypeName"] = _ObjectTypeName;
                obj["ObjectId"] = obj["Id"];
                $('#gridTable1').addRowData(_rowid, obj);
                _rowid++;
            }
        });
        //查询事件
        $("#btn_Search").click(function () {
            var queryJson = new Object();
            queryJson["organizeId"] = organizeId;
            queryJson["Type"] = _ObjectTypeId;
            queryJson["keyword"] = $("#txt_Keyword").val();
            $gridTable.jqGrid('setGridParam', {
                url: "../../DailyEventManage/OpenShop/GetObject",
                postData: queryJson
            }).trigger('reloadGrid');
        });
        //查询回车
        $('#txt_Keyword').bind('keypress', function (event) {
            if (event.keyCode == "13") {
                $('#btn_Search').trigger("click");
            }
        });
    }

    //加载表格
    function GetGrid1() {
        var selectedRowIndex = 0;
        var $gridTable = $('#gridTable1');
        $gridTable.jqGrid({
            datatype: "json",
            height: $(window).height() - 141.5,
            autowidth: true,
            colModel: [
                { label: "主键", name: "ObjectId", index: "ObjectId", hidden: true },
                { label: "类型", name: "ObjectTypeId", index: "ObjectTypeId", hidden: true },
                { label: "类型名", name: "ObjectTypeName", index: "ObjectTypeName", hidden: true },
                {
                    label: " ", name: "delete", index: "delete", width: 30, align: "center",
                    formatter: function (cellvalue, options, rowObject) {
                        return "<a onclick=\"btn_delete_Obj(" + options.rowId + ");\"><i class=\"fa fa-trash\"></i>&nbsp;</a>"
                    }
                },
                { label: "编号", name: "Code", index: "Code", width: 50, align: "center" },
                { label: "名称", name: "Name", index: "Name", width: 100, align: "center" },
                { label: "位置描述", name: "OtherCode", index: "OtherCode", width: 50, align: "center" }
            ],
            viewrecords: true,
            rownumbers: true,
            shrinkToFit: false,
            hidegrid: false,
            unwritten: false,
            onSelectRow: function (status) {
                selectedRowIndex = $("#" + this.id).getGridParam('selrow');
            },
            gridComplete: function () {
                $("#" + this.id).setSelection(selectedRowIndex, false);
            }
        });
    }

    function TopMost() {
        //var $tr = $(obj).parent().parent();
        var id = $('#gridTable1').jqGrid('getGridParam', 'selrow');
        if (checkedRow(id)) {
            $("#gridTable1 tr td.sgexpanded").trigger("click");
            var $tr = $('#gridTable1 tr#' + id);
            if ($tr.index() != 1)
                $tr.prevAll("tr:last").after($tr);
            $tr.getGridParam('selrow');
        }
    }

    function SortUp() {
        //var $tr = $(obj).parent().parent();
        var id = $('#gridTable1').jqGrid('getGridParam', 'selrow');
        if (checkedRow(id)) {
            $("#gridTable1 tr td.sgexpanded").trigger("click");
            var $tr = $('#gridTable1 tr#' + id);
            if ($tr.index() != 1)
                $tr.prev().before($tr);
            $tr.getGridParam('selrow');
        }
    }


    function SortDown() {
        //var $tr = $(obj).parent().parent();
        var id = $('#gridTable1').jqGrid('getGridParam', 'selrow');
        if (checkedRow(id)) {
            $("#gridTable1 tr td.sgexpanded").trigger("click");
            var $tr = $('#gridTable1 tr#' + id);
            $tr.next().after($tr);
            $tr.getGridParam('selrow');
        }
    }

    function DownMost() {
        //var $tr = $(obj).parent().parent();
        var id = $('#gridTable1').jqGrid('getGridParam', 'selrow');
        if (checkedRow(id)) {
            $("#gridTable1 tr td.sgexpanded").trigger("click");
            var $tr = $('#gridTable1 tr#' + id);
            $tr.nextAll("tr:last").after($tr);
            $tr.getGridParam('selrow');
        }
    }

    //移除
    function btn_delete_Obj(rowid) {
        if (rowid) {
            dialogConfirm('注：您确定要移除吗？该操作将无法恢复？', function (r) {
                if (r) {
                    $("#gridTable1").delRowData(rowid);
                    dialogMsg("移除成功。", 1);
                }
            });
        }
        else {
            dialogMsg('请选择需要移除的数据！', 0);
        }
    }

    //保存表单
    function AcceptClick() {
        if (!$('#form1').Validform()) {
            return false;
        }
        var $gridTable = top.frames["Form1"].$("#gridTable");
        var obj = $("#gridTable1").jqGrid("getRowData");
        $.each(obj, function (index, data) {
            $gridTable.addRowData($_rowid, data);
            $_rowid++;
        });
        top.frames["Form1"]._rowid = $_rowid;
        //$gridTable[0].addJSONData(obj);
        //var Objects = $("#Object option");
        //Objects.each(function () {
        //    var postData = $("#form1").GetWebControls("");
        //    postData["ObjectTypeId"] = _ObjectTypeId;
        //    postData["ObjectTypeName"] = _ObjectTypeName;
        //    postData["ObjectId"] = $(this).attr("value");
        //    postData["ObjectName"] = $(this).text();
        //    postData["ContentId"] = "";
        //    postData["ContentCode"] = "";
        //    postData["ContentType"] = "";
        //    postData["TypeDescription"] = "";
        //    $gridTable.addRowData(($gridTable.jqGrid("getRowData").length + 1), postData);
        //});
        dialogMsg("操作成功。", 1);
        dialogClose();
    }
</script>
<div id="SelectType" style="margin-top: 20px; margin-right: 30px;">
    <table class="form">
        <tr>
            <th class="formTitle">对象种类</th>
            <td class="formValue">
                <div id="ObjectTypeId" class="btn-group">
                    <a class="btn btn-default dropdown-text" data-toggle="dropdown" data-value="0" style="width:230px;">房间</a>
                    <a class="btn btn-default dropdown-toggle" data-toggle="dropdown"><span class="caret"></span></a>
                    <ul class="dropdown-menu" style="width:230px;">
                        <li><a data-value="0">房间</a></li>
                        <li><a data-value="1">商户</a></li>
                        <li><a data-value="2">公共部位</a></li>
                        <li><a data-value="3">设备</a></li>
                        <li><a data-value="4">装修现场</a></li>
                    </ul>
                </div>
            </td>
            <th class="formTitle"></th>
            <td class="formValue"></td>
        </tr>
        <tr>
            <th class="formTitle" colspan="2" style="padding-left: 30px; padding-top: 5px;"><input type="text" id="txt_Keyword" style="width:78%;float:left;height:31px;" class="form-control" placeholder="请输入编号或者名称" /><a id="btn_Search" class="btn btn-primary"><i class="fa fa-search"></i>&nbsp;查询</a></th>
            <th class="formValue" style="left: 0px; text-align: left; padding-top: 5px;" colspan="2"></th>
        </tr>
        <tr>
            <th class="formTitle" colspan="2" style="padding-top: 10px; padding-left: 30px;">
                <div style="width:80%;float:left;"><table id="gridTable"></table></div>
                <div style="width: 20%; float: right; text-align: center; line-height:338px;height:338px;"><i class="fa fa-exchange"></i></div>
            </th>
            <th class="formTitle" colspan="2" style="padding-top:10px;">
                <div style="width:90%;float:left;">
                    <table id="gridTable1"></table>
                </div>
                <div style="width: 10%; float: right;">
                    <a class="btn btn-default" onclick="TopMost();"><i class="fa fa-arrow-up"></i>最上</a><br />
                    <a class="btn btn-default" onclick="SortUp();"><i class="fa fa-arrow-up"></i>向上</a><br />
                    <a class="btn btn-default" onclick="SortDown();"><i class="fa fa-arrow-down"></i>向下</a><br />
                    <a class="btn btn-default" onclick="DownMost();"><i class="fa fa-arrow-down"></i>最下</a><br />
                </div>
            </th>
        </tr>
    </table>
</div>
